require('fs');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WebpackChunkHash = require('webpack-chunk-hash');

module.exports = (config, env) => {
  config.module.rules[0].exclude.push(/\.ejs$/); // 把index.ejs加入白名单 避免 html-webpack-plugin 将其转为base64编码
  if (env === 'production') {
    config.output.filename = '[name].[chunkhash].js';
    config.output.chunkFilename = '[chunkhash].async.js';
    config.plugins[2] = new ExtractTextPlugin({
      filename: '[contenthash:20].css',
      allChunks: true,
    }); // 覆盖roadhog的插件配置
    config.plugins[4] = new HtmlWebpackPlugin({ // 覆盖roadhog的插件配置
      template: 'ejs!src/index.ejs',
      inject: true,
      minify: { collapseWhitespace: true },
      production: true,
    });
    config.plugins.push(new WebpackChunkHash({ algorithm: 'md5' })); // 使用chunkHash
  }
  return config;
};
